#include "TankWars.h"

#include <io.h>
#include <fcntl.h>

#pragma comment(lib, "opengl32")
#pragma comment(lib, "glu32")
#pragma comment(lib, "glew32")
#pragma comment(lib, "SDL")
//#pragma comment(lib, "SDLmain")
#pragma comment(lib, "SDL_image")





void ActivateConsole()
{
    AllocConsole();
    
    HANDLE newConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE newConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    int inFd = _open_osfhandle((long)newConsoleInput, _O_TEXT);
    int outFd = _open_osfhandle((long)newConsoleOutput, _O_TEXT);
    
    FILE* consoleIn = _fdopen(inFd, "r");
    FILE* consoleOut = _fdopen(outFd, "w");
    
    setvbuf(consoleIn, NULL, _IONBF, 0);
    setvbuf(consoleOut, NULL, _IONBF, 0);
    
    *stdin = *consoleIn;
    *stdout = *consoleOut;
}

int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{

	ActivateConsole();
	return main(0,0);
}


void cleanup()
{
	SDL_Quit();
}


int main(int argc, char ** argv)
{


	

	atexit(cleanup);
	
	
	TankWarsWindow tw;

	

	wgWindow::StartMainLoop();

	return 0;
}


TankWarsWindow::TankWarsWindow()
{
	CreateAppWindow(800, 600, "Tank Wars");

	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		/* Problem: glewInit failed, something is seriously wrong. */
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		exit(-1);
	}
	fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
	
	if (glewIsSupported("GL_VERSION_2_0"))
		printf("Ready for OpenGL 2.0\n");
	else {
		printf("OpenGL 2.0 not supported\n");
		exit(-1);
	}
	

	glClearColor(.5, .5, 1, 1);

	glEnable(GL_DEPTH_TEST);
	glClearDepth(1);
	glDepthFunc(GL_LEQUAL);

	arena = new BattleArena();
	arena->Load("levels/0/");

}
void TankWarsWindow::Render()
{
	//timer.WaitVSync();
	float lastFrameTime = timer.getSeconds(timer.Update());
	

	float fps = 1.0f / lastFrameTime;

	char title[20];
	sprintf_s(title, 20, "FPS = %.1f", fps);

	SDL_WM_SetCaption(title, 0);

	Update(lastFrameTime);

	//glClearColor((clock() % 1000) / 1000.0f, 0,0,1);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();

	
	arena->Render();

	


	SDL_GL_SwapBuffers();
}

TankWarsWindow::~TankWarsWindow()
{
	if(arena != 0)
	{
		delete arena;
		arena = 0;
	}
}

void TankWarsWindow::HandleKeyboard(unsigned char key, int mx, int my)
{
	wgCamera& camera=arena->getCamera();
	vector3_t cam_move;
	switch(key)
	{
	case 'w':
	case 'W':
		keyboardFlags |= KB_MOVE_FWD;
		break;
	case 's':
	case 'S':
		keyboardFlags |= KB_MOVE_BACK;
		break;
	case 'a':
	case 'A':
		keyboardFlags |= KB_MOVE_LT;
		break;
	case 'd':
	case 'D':
		keyboardFlags |= KB_MOVE_RT;
		break;

	}
}
void TankWarsWindow::HandleKeyboardSpecial(int key, int, int)
{
	switch(key)
	{
	case SDLK_LEFT:
		keyboardFlags |= KB_ROT_LEFT;break;
	case SDLK_RIGHT:
		keyboardFlags|= KB_ROT_RIGHT;break;
	case SDLK_UP:
		keyboardFlags|=KB_ROT_UP;
		break;
	case SDLK_DOWN:
		keyboardFlags|=KB_ROT_DOWN;break;
	}


}

void TankWarsWindow::Update(float frameTime)
{

	int numkeys;
	UINT8 * keys = SDL_GetKeyState(&numkeys);


	wgCamera& cam = arena->getCamera();
	vector3_t v;
	if(keys[SDLK_w])
		v.z = -frameTime;
	else if(keys[SDLK_s])
		v.z = frameTime;
	if(keys[SDLK_a])
		v.x = - frameTime;
	else if(keys[SDLK_d])
		v.x = frameTime;
	if(keys[SDLK_LEFT])
		cam.RotateY(-PI / 2.0f * frameTime);
	else if(keys[SDLK_RIGHT])
		cam.RotateY(PI / 2.0f * frameTime);
	
	

	if(v.x != 0.0f || v.y != 0.0f || v.z != 0.0f)
	{
		v = v * 500.0;
		cam.MoveForward(v.z);
		cam.Strafe(v.x);
	}



	keyboardFlags = 0;
}